package com.scau.ums.service.imp.login;


import com.scau.ums.mapper.AdminMapper;
import com.scau.ums.mapper.MaintainerMapper;
import com.scau.ums.mapper.StudentMapper;
import com.scau.ums.mapper.TeacherMapper;
import com.scau.ums.model.User;
import com.scau.ums.service.Login;
import com.scau.ums.service.imp.password.CommonPasswordImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * author: 莫伟杰
 * date： 2019/7/18 10:39
 */
@Service
public class LoginImp implements Login {

    private AdminMapper adminMapper;
    private StudentMapper studentMapper;
    private TeacherMapper teacherMapper;
    private MaintainerMapper maintainerMapper;
    private CommonPasswordImp commonPasswordImp;

    @Autowired
    public LoginImp(AdminMapper adminmapper,StudentMapper studentMapper,TeacherMapper teacherMapper,MaintainerMapper maintainerMapper)
    {
        this.adminMapper=adminmapper;
        this.studentMapper=studentMapper;
        this.teacherMapper=teacherMapper;
        this.maintainerMapper=maintainerMapper;
    }

    @Autowired
    public void setCommonPasswordImp(CommonPasswordImp commonPasswordImp) {
        this.commonPasswordImp = commonPasswordImp;
    }

    @Override
    public User login(String id, String pwd,int loginType) {
        User user;
        switch (loginType)
        {
            case User.ADMIN :user=adminMapper.selectByPrimaryKey(id);break;
            case User.STUDENT :user=studentMapper.selectByPrimaryKey(id);break;
            case User.TEACHER :user=teacherMapper.selectByPrimaryKey(id);break;
            case User.MAINTAINER :user=maintainerMapper.selectByPrimaryKey(id);break;
            default:user=null;
        }
        if(user!=null&&commonPasswordImp.matches(pwd,user.getPassword()))
        {
            System.out.println("登录成功！");
            return user;
        }
        else
        {
            System.out.println("登录失败！");
            return null;
        }
    }

}
